﻿@using si.caha.Repository;

@{
    Layout = "/tpl/_layout.cshtml";
    PageData["PageTitle"] = "Orders list";
    var dataContext = new DataContext();

    var orderRepository = new OrderRepository(dataContext);
    var grid = new WebGrid(orderRepository.GetAll(), ajaxUpdateContainerId: "grid", defaultSort: "OrderDate");

    var orderId = UrlData[0].IsInt() ? UrlData[0].AsInt() : 0;

    if (orderId > 0)
    {
        var userRepository = new UserRepository(dataContext);
        var addressRepository = new AddressRepository(dataContext);
        var itemRepository = new ItemRepository(dataContext);
        
        var order = orderRepository.Get(orderId);
        var user = userRepository.Get(order.UserId);
        var address = addressRepository.Get(order.AddressId);
    }
}
@functions {
   string isChecked(bool chk)
   {
      return chk ? "checked" : "";
   }
}
<script>
$(function(){
    $("#message").hide();

    $(":checkbox").change(function () {
        var orderId = $(this).val();
        $.post("/Admin/Orders/partial/saveOrder.cshtml", { completed: orderId }, 
            function(){
                $("#message").show();
        });
    });
});
</script>
<span id="message">Saved</span>
<div id="grid">
    <form id="ordersForm">
    @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "gridhead",
        alternatingRowStyle: "altRow",
        columns: grid.Columns(
            grid.Column("Order", format:@<a href="@Href("~/Admin/Orders/Details/" + @item.OrderId)">Show details</a>),
            grid.Column("OrderDate"),
            grid.Column("Completition", 
                format:@<input type="checkbox" name="completed" id="completed" value="@item.OrderId" @isChecked(item.Completed) />)
        )
    )
    </form>
</div>